package com.thunder.soft.ui.activity;

import android.content.Intent;
import android.widget.LinearLayout;

import androidx.lifecycle.Observer;

import com.just.agentweb.AgentWeb;
import com.thunder.soft.R;
import com.thunder.soft.common.base.BaseActivity;
import com.thunder.soft.common.util.ViewModelProviderHelper;
import com.thunder.soft.databinding.ActivityDetailsBinding;
import com.thunder.soft.vm.DetailsViewModel;

public class DetailsActivity extends BaseActivity<ActivityDetailsBinding> {
    private String url;
    private AgentWeb agentWeb;
    private DetailsViewModel detailsViewModel;

    @Override
    protected void initData() {
        detailsViewModel = ViewModelProviderHelper.buildViewModel(this, DetailsViewModel.class);
        binding.setDetailsViewModel(detailsViewModel);
        getLifecycle().addObserver(detailsViewModel);

        Intent intent = getIntent();
        url = intent.getStringExtra("link");
        agentWeb = AgentWeb.with(this)
                .setAgentWebParent(binding.root, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .createAgentWeb()
                .ready()
                .go(url);

        // 关闭页面
        detailsViewModel.getFinishActivity().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                finish();
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_details;
    }
}
